package com.lang.dao.impl;

import com.lang.dao.OrderDao;
import com.lang.entity.Address;
import com.lang.entity.Item;
import com.lang.entity.Orders;
import com.lang.entity.Product;
import com.lang.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * @Date : 2024/3/14 10:47
 * @Created by Lang
 * @ProjectName : LclShop
 */
public class OrderDaoImpl implements OrderDao {
    private QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public int addOrder(Orders orders) {
        String sql = "insert into orders(o_id,u_id,a_id,o_count,o_time,o_state) values(?,?,?,?,?,?)";
        try {
            return runner.update(sql, orders.getO_id(), orders.getU_id(), orders.getA_id(), orders.getO_count(), orders.getO_time(), orders.getO_state());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addItem(List<Item> items) {

        try {
            String sql = "insert into item(o_id,p_id,i_count,i_num) values(?,?,?,?)";
            // obs用于存放所有的一个用户的购物车的所有订单详情
            Object[][] obs = new Object[items.size()][];
            // 将每一个订单详情存放到obs里面
            for (int i = 0; i < items.size(); i++) {
                Item item = items.get(i);
                obs[i] = new Object[]{item.getO_id(), item.getP_id(), item.getI_count(), item.getI_num()};
            }
            int[] batch = runner.batch(sql, obs);
            return batch.length;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Orders> showOrder(int uid) {
        String sql = "select * from orders o inner join address a on o.a_id=a.a_id and o.u_id=?";
        try {
            List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), uid);
            List<Orders> orderList = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Orders o = new Orders();
                BeanUtils.populate(o, map);

                Address a = new Address();
                BeanUtils.populate(a, map);

                o.setAddress(a);
                orderList.add(o);
            }
            return orderList;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public Orders showOrderByOid(String oid) {
        String sql = "select * from orders o inner join address a on o.a_id=a.a_id and o.o_id=?";
        try {
            Map<String, Object> map = runner.query(sql, new MapHandler(), oid);
            Orders orders = new Orders();
            BeanUtils.populate(orders, map);

            Address address = new Address();
            BeanUtils.populate(address, map);
            orders.setAddress(address);
            return orders;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Item> getItems(String oid) {
        try {
            String sql = "select * from item i inner join product p on i.p_id=p.p_id and i.o_id=?";
            List<Map<String, Object>> map = runner.query(sql, new MapListHandler(), oid);
            List<Item> items = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : map) {
                Item item = new Item();
                BeanUtils.populate(item, stringObjectMap);

                Product product = new Product();
                BeanUtils.populate(product,stringObjectMap);
                item.setProduct(product);
                items.add(item);
            }
            return items;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public int deleteOrderByOid(String oId) {
        String sql = "delete from orders where o_id = ?";
        try {
            return runner.update(sql, oId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOrderByUid(String id) {
        String sql = "delete from orders where u_id = ?";
        try {
            return runner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
